﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RockCompany.Common.Lib;
using RockCompany.Common.Constant;
using RockCompany.Master.Model;
using RockCompany.Master.Controller;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Common.Forms;
using RockCompany.Master.Validator;

namespace RockCompany.Master.View
{
    public partial class SW02 : ParentFrm
    {
        #region Properties
        private Translator trans;
        private UserAuthenticate uAuth;
        private SW02FormModel frmModel;
        private string sUserName;
        private DataTable woList;
        private bool isChooser = false;
        #endregion

        #region Constructor
        public SW02()
        {
            InitializeComponent();
            initDefaultData();
        }

        public SW02(Translator trans, string sUname, Form caller)
            : base(sUname, caller)
        {
            sUserName = (string.IsNullOrEmpty(sUname) ? "" : sUname);
            this.trans = trans;
            Owner = caller;
            InitializeComponent();
            initDefaultData();            
        } 
        #endregion

        #region Utility Methods
        private void initDefaultData()
        {
            frmModel = new SW02FormModel();
            frmModel.Owner = this;
            trans.controlTranslate(this);
            frmModel.translator = this.trans;
            uAuth = new UserAuthenticate();
            uAuth.setRights(this, sUserName);

            dateTimePicker_ToDate.Value = DateTime.Today;
            dateTimePicker_FromDate.Value = DateTime.Today.AddDays(-1);

            List<System.Reflection.PropertyInfo> properties = new List<System.Reflection.PropertyInfo>();
            NHAT_KY_CL_DTO temp1 = new NHAT_KY_CL_DTO();
            CONG_LENH_SX temp2 = new CONG_LENH_SX();
            properties.Add(temp2.GetType().GetProperty(CONG_LENH_SX.N_CHON));
            properties.Add(temp1.GetType().GetProperty(NHAT_KY_CL_DTO.N_SO_LO));
            properties.Add(temp2.GetType().GetProperty(CONG_LENH_SX.N_MA_MAU));
            properties.Add(temp1.GetType().GetProperty(NHAT_KY_CL_DTO.N_SL_TP));
            properties.Add(temp1.GetType().GetProperty(NHAT_KY_CL_DTO.N_SL_BTP));
            properties.Add(temp1.GetType().GetProperty(NHAT_KY_CL_DTO.N_SL_BTP_DIEU_CHINH));
            properties.Add(temp1.GetType().GetProperty(NHAT_KY_CL_DTO.N_SL_BTP_CON_LAI));

            List<string> readOnlyCols = new List<string>();
            readOnlyCols.Add(CONG_LENH_SX.N_MA_MAU);
            readOnlyCols.Add(NHAT_KY_CL_DTO.N_SO_LO);
            frmModel.buildDataGrid(dataGridView_WOList, properties, readOnlyCols);
            woList = frmModel.buildEmptyDataTable(properties);
            woList.Columns[NHAT_KY_CL_DTO.N_SL_BTP_CON_LAI].Expression = NHAT_KY_CL_DTO.N_SL_BTP + " - " + NHAT_KY_CL_DTO.N_SL_BTP_DIEU_CHINH;
        }

        // Interface with SW01A - New Work Order
        public void callChooser(bool b)
        {
            isChooser = b;
            btSelect.Enabled = b;
            button_Delete.Enabled = !b;
            button_Modify.Enabled = !b;
            button_New.Enabled = !b;
        }

        public DataTable getSelectedBTPs()
        {
            List<DataRow> lst = Util.GetSelectedRows(this.woList);
            DataTable dt = this.woList.Clone();
            foreach (DataRow row in lst)
            {
                dt.ImportRow(row);
            }
            return dt;
        }
        #endregion

        #region Event Methods
        private void button_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        // Link QL Cong lenh form
        private void button_New_Click(object sender, EventArgs e)
        {
            SW01B matsWorksManage = new SW01B(trans, sUserName, this);
            matsWorksManage.callChooser(true);
            if (matsWorksManage.ShowDialog(this) == DialogResult.OK)
            {
                List<string> selectedWorkOrder = matsWorksManage.getSelectedWO();
                DataTable tempDT = frmModel.searchWorkOrder(selectedWorkOrder);
                matsWorksManage.Dispose();
                List<string> lst = new List<string>();
                lst.Add(NHAT_KY_CL_DTO.N_SO_LO);
                lst.Add(CONG_LENH_SX.N_MA_MAU);
                frmModel.copyDataTable(tempDT, this.woList, lst);
                Util.MarkSelected(this.woList);
                dataGridView_WOList.DataSource = this.woList;
            }
        }

        private void button_Search_Click(object sender, EventArgs e)
        {
            string sfrom = dateTimePicker_FromDate.Value.ToString("yyyyMMdd");
            string sto = dateTimePicker_ToDate.Value.ToString("yyyyMMdd");
            DataTable tempDT = frmModel.searchTienDoCL(textBox_SoLo.Text, sfrom, sto);
            if (CommonModel.existData(tempDT))
            {
                this.woList.Rows.Clear();// Clear a previous search results
                frmModel.copyDataTable(tempDT, this.woList, Util.ColumnNames(this.woList));
                // Set row state = modified
                foreach (DataRow r in this.woList.Rows)
                {
                    r.AcceptChanges();
                    r.SetModified();
                }
                dataGridView_WOList.DataSource = this.woList;
            }
        }

        private void button_Modify_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION)
                                                                         , trans.getMessageCaption(CommonConstantIF.MODIFY_CONFIRM)) &&
                frmModel.Upsert(this.sUserName, this.woList))
            {
                InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.OPERATION_SUCCESS), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
            }
            else
            {
                InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.OPERATION_FAILURE), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
            }

        }


        private void button_Delete_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION),
                                             trans.getMessageCaption(CommonConstantIF.CONFIRM_DELETE)))
            {
                if (frmModel.removeNhatKy(Util.GetSelectedRows(this.woList), this.woList))
                {
                    InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.OPERATION_SUCCESS), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                }
                else
                {
                    InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.OPERATION_FAILURE), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                }
            }
        }

        private void button_PrintRP_Click(object sender, EventArgs e)
        {
            if (frmModel.export2Excel(InforDialog.showSaveFileDlg(this), this.woList))
            {
                InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.SaveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
            }
            else
                InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.SaveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
        }

        private void dataGridView_WOList_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            int num1_index = dataGridView_WOList.CurrentRow.Cells[NHAT_KY_CL_DTO.N_SL_BTP].ColumnIndex;
            int num2_index = dataGridView_WOList.CurrentRow.Cells[NHAT_KY_CL_DTO.N_SL_BTP_CON_LAI].ColumnIndex;
            int num3_index = dataGridView_WOList.CurrentRow.Cells[NHAT_KY_CL_DTO.N_SL_BTP_DIEU_CHINH].ColumnIndex;
            int num4_index = dataGridView_WOList.CurrentRow.Cells[NHAT_KY_CL_DTO.N_SL_TP].ColumnIndex;
            if (e.ColumnIndex == num1_index || e.ColumnIndex == num2_index || e.ColumnIndex == num3_index || e.ColumnIndex == num4_index)
            {
                string s = UserValidator.isValidNumer(e.FormattedValue);
                e.Cancel = !string.IsNullOrEmpty(s);
                if (e.Cancel)
                {
                    InforDialog.showWarningrBox(Owner, trans.getMessageCaption(s), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }
        #endregion
    }
}
